<template>
	<view class="costManage-container">
		<view class="date-choose-box">
			<image src="@/static/index/calendar.png" mode="widthFix" class="calendar-icon"></image>
			<picker mode="date" fields="month" :data-index='0' :value="starTime" @change="bindDateChange" :end="endTime">
				<view class="select-input" v-if="starTime">{{starTime}}</view>
				<view class="select-input" v-else>开始日期</view>
			</picker>
			~
			<picker mode="date" fields="month" :data-index='1' :value="date" @change="bindDateChange" :start="starTime" :end="currentDate">
				<view class="select-input" v-if="endTime">{{endTime}}</view>
				<view class="select-input" v-else>结束日期</view>
			</picker>
		</view>
		
		<view class="cost-box">
			<view class="cost-item total-cost">
				<text class="value">{{info.totalCost||0}}</text>
				<text class="label">总成本</text>
			</view>
			<view class="item-box flexWrapNo">
				<view class="cost-item">
					<text class="value">{{info.tuanCharges||0}}</text>
					<text class="label">美团手续费</text>
				</view>
				<view class="cost-item">
					<text class="value">{{info.douyinCharges||0}}</text>
					<text class="label">抖音手续费</text>
				</view>
				<view class="cost-item">
					<text class="value">{{info.platformCharges||0}}</text>
					<text class="label">系统服务费</text>
				</view>
				<view class="cost-item">
					<text class="value">{{info.bankCharges||0}}</text>
					<text class="label">银行手续费</text>
				</view>
			</view>
		</view>
		
		<!-- 列表数据大于0 -->
		<view class="table-list" v-if="list.length>0">
			<view class="table-item table-head">
				<view class="date">时间</view>
				<view class="type">费用类型</view>
				<view class="money">费用金额</view>
				<view class="operate">操作</view>
			</view>
			<view class="table-item" v-for="(item,index) in list" :key="index">
				<view class="date">{{item.costDate}}</view>
				<view class="type">{{formatType(item.type)}}</view>
				<view class="money">{{item.amount}}</view>
				<view class="operate">
					<view class="edit" @tap="goPage(`/micro-shareholder/addAndEditCost?id=${item.id}`)" v-if="sameMonth(item.costDate)">编辑</view>
					<view class="del" @tap="handleDelete(item.id)" v-if="sameMonth(item.costDate)">删除</view>
				</view>
			</view>
		</view>
		<view class="tips" v-if="list.length>0">
			截止每月月底最后一天23:59:59，可上传或修改本月的成本记录。不可上传或修改之前月份的成本记录，请及时填报并核对数据。
		</view>
		
		<view class="quesheng" v-if="list.length==0">
			<image src="https://static.mo-yu.vip/wechat/images/orderEmpty.png"></image>
		</view>
		
		<view class="bottom-btn">
			<view class="btn" @tap="goPage('/micro-shareholder/addAndEditCost')">新增</view>
		</view>
		
		<!--操作指引悬浮按钮 -->
		<guideline :path="'micro-shareholder/costManage'"></guideline>
	</view>
</template>

<script>
	export default{
		data(){
			const currentDate = this.getDate({
				format: true
			})
			return{
				starTime:currentDate,
				endTime:currentDate,
				currentDate:currentDate,
				info:{},
				list:[],
				typeArray:[
					{label:'电费',value:1},
					{label:'水费',value:2},
					{label:'保洁费',value:3},
					{label:'维护费',value:4},
					{label:'房租',value:5},
					{label:'自采',value:6},
					{label:'商城采购',value:7},
					{label:'其他',value:9},
				],
			}
		},
		
		onShow() {
			this.getList()
			// console.log( this.sameMonth('2024-09-30'))
		},
		
		methods:{
			// 获取日期 --当月
			getDate(type) {
				const date = new Date();
				date.setDate(date.getDate());
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				// return `${year}-${month}-${day}`
				return `${year}-${month}`
			},
			
			// 选择时间
			bindDateChange(e) {
				let index = e.currentTarget.dataset.index
				if (index == 0) {
					this.starTime = e.target.value
				} else {
					this.endTime = e.target.value
				}
				this.getList()
			},
			
			// 判断是否在同一个月
			sameMonth(date) {    
				let date1 = new Date(date); //将传入的时间字符串转换成时间对象    
				let today = new Date(); //当前时间   
				let firstDay =  new Date(today.getFullYear(), today.getMonth(), 1)   //计算出当月第一天    
				let lastDay = new Date(today.getFullYear(), today.getMonth()+1, 0, 23, 59, 59)   //计算出当月最后一天
				
			    if (date1.getTime() < firstDay.getTime() || date1.getTime() > lastDay.getTime()) {
					return false;  //不在同一个月内    
				} else {        
					return true;  //在同一个月内    
				}
			},
			
			// 获取成本明细列表
			getList(){
				this.$request.post(this.$api.costList, {
					storeId:uni.getStorageSync('store_id'),
					start:this.starTime,
					end:this.endTime,
				}, {
					isJava: true
				}).then(res => {
					if(res.code==200){
						let {
							data = {}
						} = res;
						this.info=data
						this.list=data.costList
					}
				})
			},
			
			
			// 回显费用类型
			formatType(value){
				let foundData = this.typeArray.find(item => item.value ==value)
				return foundData.label
			},
			
			// 删除
			handleDelete(id){
				uni.showModal({
					title: '提示',
					content: '是否删除该条成本数据？',
					confirmText:'确定删除',
					confirmColor:'#45B250',
					success: res=> {
						if (res.confirm) {
							this.$request.get(this.$api.delCostDetail, {
								id:id,
								isJava:true,
							}).then(res => {
								if(res.code==200){
									this.$tools.showToast('删除成功')
									this.getList()
								}
							})
						} 
					}
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.costManage-container{
		padding: 0 28rpx;
		padding-bottom: 130rpx;
	}
	.date-choose-box{
		color: #FFFFFF;
		display: flex;
		align-items: center;
		gap:20rpx;
		padding: 28rpx 0 20rpx 0;
		font-size: 28rpx;
		background: #040336;
		position: sticky;
		top: 0;
		.calendar-icon{
			width: 36rpx;
		}
	}
	.cost-box{
		padding-bottom: 40rpx;
		border-bottom: 1rpx solid rgba(255,255,255,0.3);
		.cost-item{
			display: flex;
			flex-direction: column;
			align-items: center;
			.value{
				color:rgba(255,255,255,1);
				font-size: 28rpx;
				font-weight: bold;
			}
			.label{
				margin-top: 4rpx;
				color:rgba(255,255,255,0.8);
				font-size: 26rpx;
			}
		}
		.total-cost{
			.value{
				font-size: 34rpx;
			}
		}
		.item-box{
			margin-top: 20rpx;
			gap: 20rpx;
			justify-content: space-between;
		}
	}
	
	.table-list{
		margin-top: 30rpx;
		background: #1D1C5C;
		border-radius: 8rpx;
		.table-item{
			display: flex;
			color: #fff;
			font-size: 24rpx;
			text-align: center;
			border-bottom:1rpx solid  rgba(255,255,255,0.3);
			&:last-child{
				border-bottom: unset;
			}
			>view{
				padding: 16rpx 10rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.date{
				width: 170rpx;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.type{
				width: 170rpx;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.money{
				flex: 1;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.operate{
				width: 130rpx;
				gap: 10rpx;
				.edit{
					color: #26BCFD;
				}
				.del{
					color: #d12920 ;	
				}
			}
		}
		.table-head{
			>view{
				font-weight: bold;
				font-size: 26rpx !important;
				padding: 14rpx 10rpx;
			}
		}	
	}
	.tips{
		margin-top: 10rpx;
		margin-bottom: 30rpx;
		font-size: 24rpx;
		color: #FFE577;
	}
	
	.quesheng{
		margin: 50rpx 0;
		display: flex;
		justify-content: center;
		image{
			width: 476upx;
			height: 280upx;
		}
	}
	
	.bottom-btn{
		width: 100%;
		position: fixed;
		bottom: 40rpx;
		left: 0;
		.btn{
			width: 670rpx;
			height: 80rpx;
			margin: 0 auto;
			background: rgb(31, 124, 247);
			border-radius: 72rpx;
			font-size: 34rpx;
			color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}
</style>